<?php
/**
 * Description of ResourceCollection
 *
 * @author Gowork
 */
class Application_Model_Acl_ResourceCollection  implements Iterator,  Countable {
    
    private $_aResources = array();
    private $_index = 0;
    
    public function __construct() {
        $this->_index = 0;
    }


    public function sort() {
        
        foreach($this->_aResources as $c=>$key) {
            $modules[] = $key['module'];
            $controllers[] = $key['controller'];
            $actions[] = $key['action'];
        }

        array_multisort($modules, SORT_ASC, $controllers, SORT_ASC, $actions, SORT_ASC, $this->_aResources);
        $this->_index = 0;
    }
    /**
     * 
     * @param mix $val
     * @param int|string $key
     * @return \Application_Acl_ResourceCollection
     */
    public function add($val,$key = null) {
        
        if(is_null($key)) {
            array_push($this->_aResources, $val);
        } else {
            $this->$key = $val;
        }
        //$this->_index++;
        return $this;
        
    }    
    
    public function setValues(array $aValues) {
        
        if(!is_array($aValues)) {
            throw new Exception('Wymagana jest zmienna typu tablicy');
        }
        array_merge_recursive($this->_aResources,$aValues);
        $this->_index+=count($aValues)-1;
        return $this;
    }
    
    public function __set($key, $val) {
        $this->_aResources[$key] = $val;
        $this->_index++;
        return $this;
    }
    
    
    public function count() {
        return (int)count($this->_aResources);
    }
    
    /**
     * 
     * @return \Application_Acl_Recources
     */
    public function current() {
        return new Application_Model_Acl_Recource($this->_aResources[$this->_index]);
    }
    
    /**
     * Zwraca aktualny klucz
     */
    public function key() {
        return $this->_index;
    }
    
    /**
     * Przesuwa index na następny element
     */
    public function next() {
        $this->_index++;
    }
        
    public function rewind() {
        $this->_index = 0;
    }
    
    /**
     * Sprawdza czy dany klucz istnieje
     * @return boolean
     */
    public function valid() {
        return (key_exists($this->_index, $this->_aResources))?true:false;
    }

    
}

?>
